#!/bin/sh

#
# This file is a part of Pcompress, a chunked parallel multi-
# algorithm lossless compression and decompression program.
#
# Copyright (C) 2012-2013 Moinak Ghosh. All rights reserved.
# Use is subject to license terms.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# moinakg@belenix.org, http://moinakg.wordpress.com/
#

usage() {
	prog=$1
	cat << _EOF
${prog} [<options>]

<options> can be one of the following:

--prefix=<val>		The installation prefix (default: /usr).
--enable-debug		Enable debug mode compilation (default: disabled).
--disable-allocator	Disable use of internal memory allocator mechanism (default: enabled).
--enable-debug-stats	Enable printing of some verbose debug info (default: disabled).
--with-openssl=<path to OpenSSL installation tree> (Default: System)
			This defaults to the system's OpenSSL library. You can use this option
			if you want to use an alternate OpenSSL installation.
--with-zlib=<path to zlib installation tree> (Default: System)
			Enable building against an alternate Zlib installation.
--with-bzlib=<path to Bzip2 library installation tree> (Default: System)
			Enable building against an alternate Bzip2 and library installation.
--with-libarchive=<path to libarchive installation tree> (Default: System)
			Enable building against an alternate libarchive installation.
--no-sse-detect		Do NOT attempt to probe the system's SSE capability for build flags. 
			Implies '--no-avx-detect' below.
--no-avx-detect		Do NOT attempt to probe the system's AVX apability for build flags.
--no-1.3-archive-compat Disable compatibility with compressed archives created with Pcompress
			version 1.3 (default: retain compatibility). Hash formats changed from
			version 1.3 to 1.4 so this option is required if files created using
			1.3 need to be decompressed by version 1.4 onwards.
--limit-key128		Limit key length to 128-bit encryption keys. Otherwise the default is to
			use 256-bit keys changeable at runtime via the '-k <keylen>' option.
--help			Display this help message.

_EOF
exit 0
}

arg1=$1
debug=0
allocator=1
debug_stats=0
prefix=/usr
libbsc_dir=./bsc
libbsc_lib=${libbsc_dir}/libbsc.a
libbsclflags='\$\(LIBBSCLFLAGS\)'
libbscwrapobj='\$\(LIBBSCWRAPOBJ\)'
libbscgenopt='\$\(LIBBSCGEN_OPT\)'
libbsccppflags='\$\(LIBBSCCPPFLAGS\)'
openssl_prefix=
openssl_libdir=
openssl_incdir=
libbz2_libdir=
libz_libdir=
libarchive_libdir=
sha256asmobjs=
sha256objs=
keylen=
yasm=yasm
yasm_params_linux="-f x64 -f elf64 -X gnu -g dwarf2 -D LINUX"
yasm_params_osx="-f macho64 --prefix=_"
yasm_params_gas_osx="-f macho64 -X gnu -p gas --prefix=_"
yasm_params_gas=
keccak_srcs=
keccak_hdrs=
keccak_srcs_asm=
extra_opt_flags=
zlib_prefix=
bzlib_prefix=
libarchive_prefix=
sse_detect=1
avx_detect=1
sse_opt_flags="-msse2"
crypto_compat_objs='\$\(CRYPTO_COMPAT_OBJS\)'
crypto_compat_flags="-D__HASH_COMPATIBILITY_"
salsa20_stream_c=
salsa20_stream_asm='\$\(XSALSA20_STREAM_ASM\)'
salsa20_debug=
so_suffix=so
crypto_asm_compile='\$\(CRYPTO_ASM_COMPILE1\)'
use_clang_as=
soname_opt="soname,"
rpath="-R"
dtag_val=',--enable-new-dtags'
lrt="-lrt"
default_opt="O3"
max_opt="O3"

if [ -x /bin/echo ]
then
	ECHO=/bin/echo
elif [ -x /usr/bin/echo ]
then
	ECHO=/usr/bin/echo
fi

if [ "x$GCC" = "x" ]
then
	GCC=gcc
fi

if [ "x$GPP" = "x" ]
then
	GPP=g++
fi

rm -rf ./buildtmp
mkdir ./buildtmp

echo "Checking for GCC ..."
# Try a simple compilation
cat << _EOF > tst.c
#include <stdio.h>

int
main(void)
{
	long l;
	printf("%lu\n", sizeof (l));
	return (0);
}
_EOF

${GCC} tst.c -o tst
if [ $? -ne 0 ]
then
	echo "ERROR:"
	echo "Cannot compile a simple program. GCC 4.4 and above is required"
	echo "to build this program. Please include installation bindir of GCC in the PATH."
	echo ""
	rm -f tst.c
	exit 1
fi

# Check bitness of system/toolchain
echo "Checking for 32-bit/64-bit platform ..."
bitness=`./tst`
if [ $bitness -lt 8 ]
then
	# Hmmm maybe default compilation is 32-bit. Re-try with m64 flag.
	${GCC} -m64 tst.c -o tst
	if [ $? -ne 0 ]
	then
		rm -f tst tst.c
		echo "ERROR:"
		echo "Only 64-bit platforms are supported."
		echo ""
		exit 1
	fi

	# If m64 compilation succeeds we assume platform to be 64-bit capable but
	# explicit flag is reqd.
	extra_opt_flags="-m64"
fi
rm -f tst tst.c

while [ "${arg1}" != "" ]
do
	case "$arg1" in
	--enable-debug) debug=1;;
	--disable-allocator) allocator=0;;
	--enable-debug-stats) debug_stats=1;;
	--prefix=*)
		pval=`echo ${arg1} | cut -f2 -d"="`
		prefix=$pval
	;;
	--with-openssl=*)
		openssl_prefix=`echo ${arg1} | cut -f2 -d"="`
	;;
	--with-zlib=*)
		zlib_prefix=`echo ${arg1} | cut -f2 -d"="`
	;;
	--with-bzlib=*)
		bzlib_prefix=`echo ${arg1} | cut -f2 -d"="`
	;;
	--with-libarchive=*)
		libarchive_prefix=`echo ${arg1} | cut -f2 -d"="`
	;;
	--use-key256)
		keylen='-DDEFAULT_KEYLEN=16'
	;;
	--no-sse-detect)
		sse_detect=0
		avx_detect=0
	;;
	--no-avx-detect)
		avx_detect=0
	;;
	--no-1.3-archive-compat)
		crypto_compat_objs=""
		crypto_compat_flags=""
	;;
	--help) usage $0;;
	*)
		echo "Unrecognized option: ${arg1}"
		exit 1
	;;
	esac
	shift
	arg1=$1
done

if [ $debug -eq 1 ]
then
	typ="DEBUG"
	salsa20_stream_c='\$\(XSALSA20_STREAM_C\)'
	salsa20_stream_asm=
	salsa20_debug='\$\(XSALSA20_DEBUG\)'
else
	typ="RELEASE"
fi

echo "Checking OS ..."
OS=$(uname)
skeinblock='\$\(SKEIN_BLOCK_C\)'
if [ "$OS" = "Linux" ]
then
	plat=$(uname -m)
elif [ "$OS" = "Darwin" ]
then
	plat=$(uname -m)
	so_suffix=dylib
elif [ "$OS" = "SunOS" ]
then
	plat=$(isainfo -v)
else
	echo "Unsupported OS: $OS"
	exit 1
fi

# Check GCC version
echo "Checking GCC version ..."
vers=`${GCC} -dumpversion`
OIFS="$IFS"
IFS=.
set -- ${vers}
IFS="$OIFS"

if [ $1 -lt 4 -o $2 -lt 4 ]
then
	echo "ERROR:"
	echo "GCC version 4.4 or above is required."
	echo ""
	exit 1
fi

if [ $sse_detect -eq 1 ]
then
	${GCC} -o sse_level ./utils/sse_level.c ./utils/cpuid.c -I./utils
	if [ $? -ne 0 ]
	then
		echo "ERROR:"
		echo "Failed to build SSE detection utility."
		echo ""
		exit 1
	fi
fi

if [ $sse_detect -eq 1 ]
then
	# SSE Detection
	${ECHO} -n "Checking for CPU SSE version ... "
	sse_ver=`./sse_level`
	if [ $? -ne 0 ]
	then
		rm -f sse_level
		echo "ERROR:"
		echo "SSE version detection utility. Try configuring with --no-sse-check option."
		echo ""
		exit 1
	fi
	echo $sse_ver
	sse_opt_flags="-m${sse_ver}"
fi

if [ $avx_detect -eq 1 -a $sse_detect -eq 1 ]
then
	${ECHO} -n "Checking for CPU AVX version ... "
	avx_ver=`./sse_level --avx`
	if [ $? -ne 0 ]
	then
		rm -f sse_level
		echo "ERROR:"
		echo "SSE/AVX version detection utility. Try configuring with --no-sse-check option."
		echo ""
		exit 1
	fi
	if [ "x$avx_ver" = "x" ]
	then
		echo None
	else
		echo $avx_ver
		sse_opt_flags="${sse_opt_flags} -m${avx_ver}"
	fi
fi
rm -f sse_level

echo $plat | egrep 'x86_64|amd64' > /dev/null
if [ $? -eq 0 ]
then
	if [ "$OS" != "Darwin" ]
	then
		skeinblock='\$\(SKEIN_BLOCK_ASM\)'
	fi
	yasm=

	#
	# Detect Yasm
	#
	echo "Checking for Yasm ..."
	for bindir in /bin /usr/bin /usr/local/bin
	do
		if [ -x ${bindir}/yasm ]
		then
			# Get yasm version
			yver=`${bindir}/yasm --version | head -1 | awk '{print $2}'`
			_OIFS=$IFS; IFS="."; set -- ${yver}; IFS="$_OIFS"
			major=$1
			minor=$2

			# Minimum yasm version 1.1
			[ $major -lt 1 -o $minor -lt 1 ] && continue
			yasm=${bindir}/yasm
			sha256asmobjs='\$\(SHA2ASM_OBJS\)'
			sha256objs='\$\(SHA2_OBJS\)'
		fi
	done
	if [ "x${yasm}" = "x" ]
	then
		echo "Yasm version 1.1 or later is required to build on x64 platforms"
		exit 1
	fi

	if [ "$OS" = "Linux" ]
	then
		yasm="${yasm} ${yasm_params_linux}"

	elif [ "$OS" = "Darwin" ]
	then
		yasm="${yasm} ${yasm_params_osx}"
		yasm_params_gas="${yasm} ${yasm_params_gas_osx}"

	elif [ "$OS" = "SunOS" ]
	then
		yasm="${yasm} ${yasm_params_linux}"
	fi

	if [ $debug -eq 1 ]
	then
		keccak_srcs='\$\(KECCAK_SRC_OPT64\)'
		keccak_hdrs='\$\(KECCAK_HDRS_OPT64\)'
	else
		keccak_srcs='\$\(KECCAK_SRC_OPT64_ASM1\)'
		if [ "$OS" != "Darwin" ]
		then
			keccak_srcs_asm='\$\(KECCAK_SRC_OPT64_ASM2\)'
		else
			keccak_srcs_asm='\$\(KECCAK_SRC_OPT64_ASM3\)'
		fi
		keccak_hdrs='\$\(KECCAK_HDRS_OPT64_ASM\)'
	fi
else
	keccak_srcs='\$\(KECCAK_SRC_OPT64\)'
fi

if [ "$OS" = "Darwin" ]
then
	crypto_asm_compile='\$\(CRYPTO_ASM_COMPILE2\)'
	use_clang_as="-Wa,-q"
	soname_opt="install_name,"
	rpath="-rpath,"
	dtag_val=""
	lrt=""
	max_opt="O2"
fi

# Detect OpenSSL library
echo "Checking for OpenSSL ..."
for lib in "${openssl_prefix}/lib64" "${openssl_prefix}/usr/lib64" \
	"${openssl_prefix}/lib" "${openssl_prefix}/usr/lib" \
	"${openssl_prefix}/ssl/lib64" "${openssl_prefix}/ssl/lib" \
	"${openssl_prefix}/lib/x86_64-linux-gnu" \
	"${openssl_prefix}/usr/lib/x86_64-linux-gnu"
do
	if [ -d ${lib} ]
	then
		if [ -f "${lib}/libcrypto.${so_suffix}" -o -h "${lib}/libcrypto.${so_suffix}" ]
		then
			openssl_libdir="${lib}"
			(cd ./buildtmp; ln -s ${openssl_libdir}/libcrypto.${so_suffix})
			break
		else
			if [ -f "${lib}/libcrypto.a" ]
			then
				openssl_libdir="${lib}"
				(cd ./buildtmp; ln -s ${openssl_libdir}/libcrypto.a)
				break
			fi
		fi
	fi
done

if [ "x${openssl_libdir}" = "x" ]
then
	echo "ERROR: OpenSSL libraries not detected."
	exit 1
fi

# Detect OpenSSL headers
for inc in "${openssl_prefix}/include" \
	"${openssl_prefix}/usr/include" \
	"${openssl_prefix}/ssl/include"
do
	if [ -d ${inc} ]
	then
		if [ -f "${inc}/openssl/sha.h" ]
		then
			openssl_incdir=${inc}
			break
		fi
	fi
done

if [ "x${openssl_incdir}" = "x" ]
then
	echo "ERROR: OpenSSL header files not detected."
	if [ "x${openssl_prefix}" = "x" ]
	then
		echo "Depending on your system you may need to install the openssl-devel or openssl-dev package."
	fi
	exit 1
fi


# Check for OpenSSL version
echo "Checking OpenSSL version ..."
cat << __EOF > tst.c
#include <stdlib.h>
#include <openssl/opensslv.h>

int
main(void)
{
	if (OPENSSL_VERSION_NUMBER < 0x0090805fL)
		exit (1);
	return (0);
}
__EOF

${GCC} ${extra_opt_flags} -I${openssl_incdir} -L${openssl_libdir} tst.c -o tst
if [ $? -ne 0 ]
then
	echo "Unable to compile OpenSSL test program please check OpenSSL installation."
	exit 1
fi
./tst
if [ $? -ne 0 ]
then
	echo "OpenSSL version too old. At least version 0.9.8e is required.\n"
	exit 1
fi

# Check for HMAC_CTX_copy function
${ECHO} -n "Checking if the OpenSSL library provides HMAC_CTX_copy function ... "
cat << __EOF > tst.c
#include <stdlib.h>
#include <openssl/sha.h>
#include <openssl/rand.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>

int
main(void)
{
	unsigned char key[16];
	HMAC_CTX *ctx = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));
	HMAC_CTX *ctx1 = (HMAC_CTX *)malloc(sizeof (HMAC_CTX));

	HMAC_CTX_init(ctx);
	HMAC_Init_ex(ctx, key, 16, EVP_sha256(), NULL);
	HMAC_CTX_copy(ctx1, ctx);

	return (0);
}
__EOF

${GCC} ${extra_opt_flags} -I${openssl_incdir} -L${openssl_libdir} -O0 -g tst.c -o tst -lcrypto >/dev/null 2>&1
if [ $? -ne 0 ]
then
	openssl_incdir="${openssl_incdir} -D__OSSL_OLD__"
	echo "No. Using internal variant."
else
	echo "Yes."
fi

rm -rf tst*
openssl_libdir="${openssl_libdir}${dtag_val}"

# Detect other library packages
for libspec in "libbz2:${bzlib_prefix}" "libz:${zlib_prefix}" "libarchive:${libarchive_prefix}"
do
	_OIFS="$IFS"
	IFS=":"
	set -- ${libspec}
	libname=$1
	pref=$2
	IFS="$_OIFS"

	echo "Checking for $libname ..."
	use_prefix="${pref}"
	if [ "x${pref}" = "x" ]
	then
		use_prefix="$prefix"
	fi
	for lib in "${pref}/lib64" "${pref}/usr/lib64" "${pref}/lib" "${pref}/usr/lib" \
		"${pref}/lib/x86_64-linux-gnu" "${pref}/usr/lib/x86_64-linux-gnu" \
		"${pref}/local/lib64" "${pref}/usr/local/lib64" "${pref}/local/lib" "${pref}/usr/local/lib" \
		"${pref}/local/lib/x86_64-linux-gnu" "${pref}/usr/local/lib/x86_64-linux-gnu" \
		"${use_prefix}/lib64" "${use_prefix}/lib" "${use_prefix}/lib/x86_64-linux-gnu" \
		"${use_prefix}/usr/lib/x86_64-linux-gnu" "${use_prefix}/.libs" "${use_prefix}"
	do
		if [ -d ${lib} ]
		then
			if [ -f "${lib}/${libname}.${so_suffix}" -o -h "${lib}/${libname}.${so_suffix}" ]
			then
				eval "${libname}_libdir=${lib}${dtag_val}"
				(cd ./buildtmp; ln -s ${lib}/${libname}.${so_suffix})
				break
			else
				if [ -f "${lib}/${libname}.a" ]
				then
					eval "${libname}_libdir=${lib}${dtag_val}"
					(cd ./buildtmp; ln -s ${lib}/${libname}.a)
					break
				fi
			fi
		fi
	done
done

if [ "x${libbz2_libdir}" = "x" ]
then
	if [ "x$bzlib_prefix" = "x" ]
	then
		echo "ERROR: Libbz2 not detected."
		echo "       You may have to install libbz2-devel or libbz2-dev"
	else
		echo "ERROR: Bzip2 library not detected in given prefix."
	fi
	exit 1
fi

if [ "x${libz_libdir}" = "x" ]
then
	if [ "x$zlib_prefix" = "x" ]
	then
		echo "ERROR: Zlib not detected."
		echo "       You may have to install libz-devel or libz-dev"
	else
		echo "ERROR: Zlib not detected in given prefix."
	fi
	exit 1
fi

if [ "x${libarchive_libdir}" = "x" ]
then
	if [ "x$libarchive_prefix" = "x" ]
	then
		echo "ERROR: Libarchive not detected."
		echo "       You may have to install libarchive-devel or libarchive-dev"
	else
		echo "ERROR: Libarchive not detected in given prefix."
	fi
	exit 1
fi

libbz2_inc=
libz_inc=
libarchive_inc=
# Detect other library headers
for hdr in "libbz2_inc:bzlib.h:${bzlib_prefix}" "libz_inc:zlib.h:${zlib_prefix}" \
	"libarchive_inc:archive.h:${libarchive_prefix}"
do
	_OIFS="$IFS"
	IFS=":"
	set -- ${hdr}
	var=$1
	hdrf=$2
	pref=$3
	IFS="$_OIFS"

	echo "Checking for $hdrf ..."
	use_prefix="${pref}"
	if [ "x${pref}" = "x" ]
	then
		use_prefix="$prefix"
	fi
	found=0
	for inc in "${pref}/include" "${pref}/usr/include" \
		"${pref}/local/include" "${pref}/usr/local/include" \
		"${use_prefix}/include" "${use_prefix}/usr/include" \
		"${use_prefix}/libarchive" "${use_prefix}"
	do
		if [ -d ${inc} ]
		then
			if [ -f "${inc}/${hdrf}" ]
			then
				eval "${var}=\"-I${inc}\""
				found=1
				break
			fi
		fi
	done
	if [ $found -ne 1 ]
	then
		echo "Cannot find header $hdrf"
		exit 1
	fi
done

echo "Checking for Libarchive version ..."
cat << _EOF > tst.c
#include <stdlib.h>
#include <archive.h>

int
main(void)
{
	int flags = ARCHIVE_READDISK_NO_TRAVERSE_MOUNTS;

        return (0);
}
_EOF

${GCC} ${extra_opt_flags} ${libarchive_inc} -L${libarchive_libdir} -O0 -g tst.c -o tst -larchive >/dev/null 2>&1
if [ $? -ne 0 ]
then
        echo "Libarchive version 3.0 or newer is required."
	exit 1
fi
rm -rf tst* tst.c

echo "Generating Makefile ..."
linkvar="LINK"
compilevar="COMPILE"
compilecppvar="COMPILE_cpp"
vecflagsvar="VEC_FLAGS"
loopoptflagsvar="LOOP_OPTFLAGS"
cppflagsvar="CPPFLAGS"
genoptvar="GEN_OPT"
noslabcppflagsvar="NO_SLAB_CPPFLAGS"
debugstatscppflagsvar="DEBUG_STATS_CPPFLAGS"
prefixvar="PREFIX"
skeinblockvar="SKEIN_BLOCK"
keylenvar="KEYLEN"

libbscdirvar="LIBBSCDIR"
libbsclibvar="LIBBSCLIB"
libbsclflagsvar="LIBBSCLFLAGS"
libbscwrapobjvar="LIBBSCWRAPOBJ"
libbscgenoptvar="LIBBSCGEN_OPT"
libbsccppflagsvar="LIBBSCCPPFLAGS"
sha256asmobjsvar="SHA2ASM_OBJS"
sha256objsvar="SHA2_OBJS"
yasmvar="YASM"
fptr_flag_var="FPTR_FLAG"
extra_opt_flags_var="EXTRA_OPT_FLAGS"
sse_opt_flags_var="SSE_OPT_FLAGS"

openssllibdirvar="OPENSSL_LIBDIR"
opensslincdirvar="OPENSSL_INCDIR"
libbz2libdirvar="LIBBZ2_DIR"
libzlibdirvar="LIBZ_DIR"
libbz2incvar="LIBBZ2_INC"
libzincvar="LIBZ_INC"
libarchivedirvar="LIBARCHIVE_DIR"
libarchiveincvar="LIBARCHIVE_INC"

keccak_srcs_var="KECCAK_SRCS"
keccak_hdrs_var="KECCAK_HDRS"
keccak_srcs_asm_var="KECCAK_SRCS_ASM"

crypto_compat_objs_var="CRYPTO_COMPAT_OBJS"
crypto_compat_flags_var="COMPAT_CPPFLAGS"
salsa20_stream_c_var="XSALSA20_STREAM_C"
salsa20_stream_asm_var="XSALSA20_STREAM_ASM"
salsa20_debug_var="XSALSA20_DEBUG"

noslabcppflagsval=
debugstatscppflagsval=

[ $allocator -eq 0 ] && noslabcppflagsval='\$\(NO_SLAB_CPPFLAGS\)'
[ $debug_stats -eq 1 ] && debugstatscppflagsval='\$\(DEBUG_STATS_CPPFLAGS\)'

cat Makefile.in | sed "
s#@GCC@#${GCC}#g
s#@GPP@#${GPP}#g
s#@CRYPTO_ASM_COMPILE@#${crypto_asm_compile}#g
s#@USE_CLANG_AS@#${use_clang_as}#g
s#@SO_SUFFIX@#${so_suffix}#g
s#@YASM_GAS@#${yasm_params_gas}#g
s#@RPATH@#${rpath}#g
s#@LRT@#${lrt}#g
s#soname#${soname_opt}#g
s#${default_opt}#${max_opt}#g
s#@DTAGS@#${dtag_val}#g
s#@${linkvar}@#\\\$\\(${typ}_${linkvar}\\)#g
s#@${compilevar}@#\\\$\\(${typ}_${compilevar}\\)#g
s#@${compilecppvar}@#\\\$\\(${typ}_${compilecppvar}\\)#g
s#@${vecflagsvar}@#\\\$\\(${typ}_${vecflagsvar}\\)#g
s#@${loopoptflagsvar}@#\\\$\\(${typ}_${loopoptflagsvar}\\)#g
s#@${cppflagsvar}@#\\\$\\(${typ}_${cppflagsvar}\\)#g
s#@${genoptvar}@#\\\$\\(${typ}_${genoptvar}\\)#g
s#@${fptr_flag_var}@#\\\$\\(${typ}_${fptr_flag_var}\\)#g
s#@${noslabcppflagsvar}@#${noslabcppflagsval}#g
s#@${debugstatscppflagsvar}@#${debugstatscppflagsval}#g
s#@${prefixvar}@#${prefix}#g
s#@${libbscdirvar}@#${libbsc_dir}#g
s#@${libbsclibvar}@#${libbsc_lib}#g
s#@${libbsclflagsvar}@#${libbsclflags}#g
s#@${libbscwrapobjvar}@#${libbscwrapobj}#g
s#@${libbscgenoptvar}@#${libbscgenopt}#g
s#@${libbsccppflagsvar}@#${libbsccppflags}#g
s#@${skeinblockvar}@#${skeinblock}#g
s#@${openssllibdirvar}@#${openssl_libdir}#g
s#@${opensslincdirvar}@#${openssl_incdir}#g
s#@${sha256asmobjsvar}@#${sha256asmobjs}#g
s#@${sha256objsvar}@#${sha256objs}#g
s#@${yasmvar}@#${yasm}#g
s#@${keylenvar}@#${keylen}#g
s#@${libbz2libdirvar}@#${libbz2_libdir}#g
s#@${libzlibdirvar}@#${libz_libdir}#g
s#@${libbz2incvar}@#${libbz2_inc}#g
s#@${libzincvar}@#${libz_inc}#g
s#@${keccak_srcs_var}@#${keccak_srcs}#g
s#@${keccak_hdrs_var}@#${keccak_hdrs}#g
s#@${keccak_srcs_var}@#${keccak_srcs}#g
s#@${keccak_srcs_asm_var}@#${keccak_srcs_asm}#g
s#@${extra_opt_flags_var}@#${extra_opt_flags}#g
s#@${sse_opt_flags_var}@#${sse_opt_flags}#g
s#@${crypto_compat_objs_var}@#${crypto_compat_objs}#g
s#@${crypto_compat_flags_var}@#${crypto_compat_flags}#g
s#@${salsa20_stream_c_var}@#${salsa20_stream_c}#g
s#@${salsa20_stream_asm_var}@#${salsa20_stream_asm}#g
s#@${salsa20_debug_var}@#${salsa20_debug}#g
s#@${libarchivedirvar}@#${libarchive_libdir}#g
s#@${libarchiveincvar}@#${libarchive_inc}#g
" > Makefile

